home *** CD-ROM | disk | FTP | other *** search
/ Die Ultimative Software-P…i Collection 1996 & 1997 / Die Ultimative Software-Pakete CD-ROM fur Atari Collection 1996 & 1997.iso / i / internet / software / tuwtcpsr / pktdrv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-25  |  3.5 KB  |  190 lines

  1. #include "cookie.h"
  2. #include "pktdrv.h"
  3. #include <tos.h>
  4. #define noDEBUG
  5. #define noVDEBUG
  6.  
  7. #ifdef DEBUG
  8. #include <stdio.h>
  9.  
  10. #include "nettrace.h"
  11. static char str[200];
  12. #endif
  13.  
  14. HADDR    bcst_haddr = { -1,-1,-1,-1,-1,-1 };        /* broadcast address */
  15. static int demux_cnt = 0;
  16.  
  17. static demux_handler demux_tab[MAXPROTOCOLS+1] = { NULL };
  18.  
  19. int (*n_reset)(void) = NULL;
  20. int (*n_info)(int,char *) = NULL;
  21. int (*n_open)(int,pkt_hndl) = NULL;
  22. int (*n_release)(int) = NULL;
  23. int (*n_send)(int,char *) = NULL;
  24. int (*n_getadr)(int,char *) = NULL;
  25. int (*n_pktfree)(char *) = NULL;
  26. char *(*n_pktalloc)(u_short) = NULL;
  27.  
  28. int net_link(void)
  29. {
  30. COOKIE *cookie;
  31. char **tmp;
  32.  
  33.         cookie = get_cookie(PKTCOOKIE);
  34.         if(!cookie || !cookie->val) return(ENOTINST);
  35.         tmp = (char **)(cookie->val);
  36.  
  37.         (char *)n_info = tmp[NETINFO];
  38.         (char *)n_open = tmp[NETOPEN];
  39.         (char *)n_release = tmp[NETRELEASE];
  40.         (char *)n_send = tmp[NETSEND];
  41.         (char *)n_getadr = tmp[NETGETADR];
  42.         (char *)n_pktalloc = tmp[NETPKTALLOC];
  43.         (char *)n_pktfree = tmp[NETPKTFREE];
  44.  
  45.         if(!n_info || !n_open || !n_release ||
  46.             !n_send || !n_getadr || !n_pktalloc || !n_pktfree)
  47.             return(ENOTINST);
  48.         return(0);
  49.  
  50. }
  51.  
  52. int net_info(int len, char *buf)
  53. {
  54. /*    net_link(); */
  55.     return(n_info(len,buf));
  56. }
  57.  
  58. int net_open(int type, pkt_hndl handler)
  59. {
  60. /*    net_link(); */
  61.     return(n_open(type,handler));
  62. }
  63.  
  64. int net_release(int handle)
  65. {
  66. /*    net_link(); */
  67.     return(n_release(handle));
  68. }
  69.  
  70. int net_send(int len, char *buf)
  71. {
  72. /*    net_link(); */
  73.     return(n_send(len,buf));
  74. }
  75.  
  76. int    net_getadr(int len,HADDR buf)
  77. {
  78. /*    net_link(); */
  79.     return(n_getadr(len,buf));
  80. }
  81.  
  82. int net_reset(void)
  83. {
  84. /*    net_link(); */
  85.     return(n_reset());
  86. }
  87.  
  88. char *net_pktalloc(u_short prot)
  89. {
  90. /*     net_link(); */
  91.     return(n_pktalloc(prot));
  92. }
  93.  
  94. int net_pktfree(char *pkt)
  95. {
  96. /*    net_link(); */
  97.     return(n_pktfree(pkt));
  98. }
  99.  
  100. int net_mux(int mod_handler_flag, demux_handler mux)
  101. {
  102.   ip_demux();
  103.   return 0;
  104. }
  105.  
  106. int net_demux(int mod_handler_flag, demux_handler mux)
  107. {
  108. register int i,loop;
  109. register int ret = 0;
  110.  
  111.     if(!mux)
  112.     {
  113. #ifdef VDEBUG
  114. sprintf(str,"> ###do demux cnt = %d\n",demux_cnt);
  115. TRACE(str);
  116. #endif
  117.         if(!demux_cnt) return(FALSE);
  118.         for(loop = 0;loop < 1; loop++)
  119.         for(i=0; i<demux_cnt; i++)
  120.         {
  121.             if(demux_tab[i])
  122.             {
  123.              ret = demux_tab[i]();
  124.             }
  125.             if(ret < 0) break;        /* network error */
  126.         }
  127. #ifdef DEBUG
  128. TRACE("< ###demux \n");
  129. #endif
  130.         return(ret);
  131.     }
  132.     else
  133.     {
  134.       if(mod_handler_flag)
  135.       {
  136. #ifdef DEBUG
  137. TRACE("> demux install\n");
  138. #endif
  139.         if(demux_cnt > MAXPROTOCOLS) return(FALSE);
  140.         demux_tab[demux_cnt] = mux;
  141.         demux_cnt++;
  142. #ifdef DEBUG
  143. TRACE("< demux \n");
  144. #endif
  145.         return(TRUE);
  146.       }
  147.       else
  148.       {
  149. #ifdef DEBUG
  150. TRACE("> demux remove\n");
  151. #endif
  152.         if(!demux_cnt) return(FALSE);
  153.         for(i=0; i<demux_cnt && demux_tab[i]==mux ;i++); /* find entry */
  154.         if(i==demux_cnt)
  155.         {
  156. #ifdef DEBUG
  157. TRACE("< demux \n");
  158. #endif
  159.           return(FALSE);
  160.         }
  161.         for(;i<demux_cnt-1;i++)
  162.             demux_tab[i] = demux_tab[i+1];    /* shift mux table */
  163.         demux_cnt--;
  164. #ifdef DEBUG
  165. TRACE("< demux \n");
  166. #endif
  167.         return(TRUE);
  168.       }
  169.     }
  170. }
  171.  
  172. /*
  173.  * Do a one's complement checksum
  174.  */
  175. u_short chksum(u_short *dp, u_short length, u_short csum)
  176. {
  177.   int len;
  178.   u_long sum;
  179.  
  180.   len = length >> 1;
  181.   sum = (u_long)csum;
  182.   while ( len-- > 0 ) sum += *dp++;
  183.   if ( length & 1 ) sum += (*dp & 0xFF00);
  184.   sum = (sum & 0xFFFF) + ((sum >> 16) & 0xFFFF);
  185.   sum = (sum & 0xFFFF) + ((sum >> 16) & 0xFFFF);
  186.  
  187.   return ( ~(u_short)(sum & 0xffffL));
  188. }
  189.  
  190.